import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:short_drama/models/message_model.dart';
import '../../../utils/http_util.dart';
import '../../models/message_list_model.dart';

class MessageViewmodel extends GetxController {
  var messages = [].obs;
  num pageNum = 1;

  refreshData(List<MessageModel> data) {
    messages.value = data;
  }

  loadMoreData(List<MessageModel> data) {
    messages.addAll(data);
  }

  Future<void> queryRefresh() async {
    try {
      final result = await HttpUtil().get(
        'member/message/list',
        params: {'page': 1, 'limit': 10},
      );
      final dynamic responseData = result.data['data']; // 明确类型声明
      messages.value = MessageListModel.fromJson(responseData).data ?? [];
      pageNum++;
    } catch (e) {
      EasyLoading.showToast('请求失败');
    }
  }

  Future<void> queryLoadMore() async {
    try {
      final result = await HttpUtil().get(
        'member/message/list',
        params: {'page': pageNum, 'limit': 10},
      );
      final dynamic responseData = result.data['data']; // 明确类型声明
      loadMoreData(MessageListModel.fromJson(responseData).data ?? []);
    } catch (e) {
      EasyLoading.showToast('请求失败');
    }
  }
}